/**
 * Created by Administrator on 2017/1/18 0018.
 */

var jsonOBJ = {
    id: null,
    majorname: null,
    classname: null,
    studentname: null,
    startTime: null,
    endTime: null,
    teachername: null,
    requestReason: null,
    authStatus: null,
    requestTime: null
};
var islogin = 1;
var isSubmit = 0;

$(function () {
    init();
});
function init() {
    $.ajax({
        url: "/huaqiao/StudentFront/getCurStu?" + new Date().getTime(),
        async: false,
        success: function (str) {
            if (checkReturn(str)) {
                $("#stu_name").text(str.name);
                $("#class_name").text(str.classname);
            }
        }
    });
    submitRequest();
}

function submitRequest() {
    $(".wt-padding button").click(function () {
        if (checkImput() == 0) {
            var st = $("#startTime").val();
            var et = $("#endTime").val();
            var reason = $("#apply-reason").val();
            var sTime = new Date(st.replace("T", " "));
            var eTime = new Date(et.replace("T", " "));
            var nTime = new Date();
            jsonOBJ.startTime = sTime;
            jsonOBJ.endTime = eTime;
            jsonOBJ.requestTime = nTime;
            jsonOBJ.requestReason = reason;
            if(isSubmit==1){
                layer.msg("请勿重复提交", {time: 1000});
                return;
            }
            $.ajax({
                url: "/huaqiao/StudentFront/addOutRequest?" + new Date().getTime(),
                type: "post",
                data: JSON.stringify(jsonOBJ),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                async: false,
                success: function (str) {
                    if(checkReturn(str)){
                        isSubmit = 1;
                        window.location.href = "apply.ok_weChat_student.html";
                    }
                }
            });
        }
    });
}

function checkImput() {
    var st = $("#startTime").val();
    var et = $("#endTime").val();
    var reason = $("#apply-reason").val();
    if (st != "" && et != "") {
        if (reason != "") {
            var sTime = new Date(st.replace("T", " "));
            var eTime = new Date(et.replace("T", " "));
            var nTime = new Date();
            if (sTime <= nTime) {
                layer.msg("起始日期不能早于当前时间", {time: 1000});
                return 3;
            } else if (sTime >= eTime) {
                layer.msg("结束日期不能早于起始日期", {time: 1000});
                return 4;
            } else {
                return 0;
            }
        } else {
            layer.msg("请假原因不能为空", {time: 1000});
            return 2;
        }
    } else {
        layer.msg("日期不能为空", {time: 1000});
        return 1;
    }
}

function checkReturn(str) {
    if (str != null && str != "") {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    layer.msg(str.errormessage, {time: 1000});
                    // window.location.href = "login_pc_student.html";
                    islogin = 0;
                }
            }
            if (str.errorcode == 0) {
                return true;
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}
